import {
    CRC16,
    stringToAsciiBytes,
    bytesToInt,
    hexStr2Bytes,
    MsgType,
    DeviceMessageSend,
    DeviceMessageReceive,
    CommonConfirmReplyMsgBody,
    RecipeSendMsgBody,
    RecipeStepInfo,
    RecipeStartCookingMsgBody,
    ElectronicScaleFoodPreparationMsgBody,
    ElectronicScaleEmptyBoxWeightMsgBody,
    ElectronicScaleSetWeightUnitMsgBody,
    FoodWeightInfo,
    RangeHoodFanControlMsgBody,
    SettingSeasoningRatioCalibrationMsgBody,
    SeasoningRatioCalibrationInfo,
    ManualAddSeasoningMsgBody,
    SeasoningAddInfo,
    SettingSeasoningBaseUnitMsgBody,
} from '@/utils/deviceProtocolUtil.js'

import {
    DeviceModel, 
    DeviceStatus,
    DevicePeripheralType,
} from '@/interface/device.js'

import { deviceProtocolListenerService } from './deviceProtocolListenerService'
import { DeviceProtocolBase } from './DeviceProtocolBase'

/**
 * 设备配置相关协议服务
 * 例如：油烟机、电子秤
 */
class DeviceProtocolEquipmentService extends DeviceProtocolBase {
    constructor() {
        super()
    }
    
    /**
     * 关闭设备链接
     * 支持电子秤、油烟机、炒菜机
     * @param {String} deviceSn 设备SN号
     */
    closeDeviceConnection(deviceSn) {
        return this.deviceProtocolStore.closeDeviceConnection(deviceSn)
    }

    /**
     * 电子秤-显示食材信息
     * @param {String} deviceSn 设备SN号
     * @param {Object} weights 各个食材盒重量
     */
    electronicScaleFoodPreparation(deviceSn, weights) {
        g_utils.writeLog("准备发送【电子秤控制-食材准备】命令给设备：", deviceSn)

        // 重量
        let { weight1, weight2, weight3, weight4 } = weights
        let msgBody = new ElectronicScaleFoodPreparationMsgBody(weight1, weight2, weight3, weight4)
    
        // 封装成发送消息
        let deviceMessageSendEntity = new DeviceMessageSend(deviceSn, MsgType.ElectronicScaleFoodPreparation, msgBody)
        let deviceReplyMsgPredicate = (msg) => {
            // msg 是一个 DeviceMessageReceive 数据类型
            g_utils.writeLog("【电子秤控制-食材准备】命令断言函数参数：", msg)
    
            let b = msg.msgBody.result > 0
            if (b) {
                g_utils.writeLog("发送【电子秤控制-食材准备】命令成功")
            } else {
                g_utils.writeLog("发送【电子秤控制-食材准备】命令失败")
            }
    
            return b
        }
        return this.deviceProtocolStore.sendMessageWithReplyPredicate(deviceMessageSendEntity, deviceReplyMsgPredicate)
    }
    /**
     * 电子秤-设置食材盒空重
     * @param {String} deviceSn 设备SN号
     * @param {number} weights 空食材盒重量
     */
    electronicScaleSetEmptyBoxWeight(deviceSn, weight) {
        g_utils.writeLog("准备发送【电子秤控制-设置食材盒空重】命令给设备：", deviceSn)
    
        // 重量
        let msgBody = new ElectronicScaleEmptyBoxWeightMsgBody(weight)
    
        // 封装成发送消息
        let deviceMessageSendEntity = new DeviceMessageSend(deviceSn, MsgType.ElectronicScaleControlSetEmptyBoxWeight, msgBody)
        let deviceReplyMsgPredicate = (msg) => {
            // msg 是一个 DeviceMessageReceive 数据类型
            g_utils.writeLog("【电子秤控制-设置食材盒空重】命令断言函数参数：", msg)
    
            let b = msg.msgBody.result > 0
            if (b) {
                g_utils.writeLog("发送【电子秤控制-设置食材盒空重】命令成功")
            } else {
                g_utils.writeLog("发送【电子秤控制-设置食材盒空重】命令失败")
            }
    
            return b
        }
        return this.deviceProtocolStore.sendMessageWithReplyPredicate(deviceMessageSendEntity, deviceReplyMsgPredicate)
    }
    /**
     * 电子秤-设置默认重量显示单位
     * @param {String} deviceSn 设备SN号
     * @param {number} unit 显示的重量单位
     */
    electronicScaleSetWeightUnit(deviceSn, unit) {
        g_utils.writeLog("准备发送【电子秤控制-设置默认重量显示单位】命令给设备：", deviceSn)
    
        // 单位
        let msgBody = new ElectronicScaleSetWeightUnitMsgBody(unit)
    
        // 封装成发送消息
        let deviceMessageSendEntity = new DeviceMessageSend(deviceSn, MsgType.ElectronicScaleControlSetWeightUnit, msgBody)
        let deviceReplyMsgPredicate = (msg) => {
            // msg 是一个 DeviceMessageReceive 数据类型
            g_utils.writeLog("【电子秤控制-设置默认重量显示单位】命令断言函数参数：", msg)
    
            let b = msg.msgBody.result > 0
            if (b) {
                g_utils.writeLog("发送【电子秤控制-设置默认重量显示单位】命令成功")
            } else {
                g_utils.writeLog("发送【电子秤控制-设置默认重量显示单位】命令失败")
            }
    
            return b
        }
        return this.deviceProtocolStore.sendMessageWithReplyPredicate(deviceMessageSendEntity, deviceReplyMsgPredicate)
    }
    
    
    /**
     * 油烟机-设置风扇灯光
     * @param {String} deviceSn 设备SN号
     * @param {number} fanSpeed 风扇转速等级
     * @param {number} lightLevel 照明灯亮度等级
     */
    rangeHoodFanControl(deviceSn, fanSpeed, lightLevel) {
        g_utils.writeLog("准备发送【油烟机-设置风扇灯光】命令给设备：", deviceSn)
        
        let msgBody = new RangeHoodFanControlMsgBody(fanSpeed,lightLevel)
    
        // 封装成发送消息
        let deviceMessageSendEntity = new DeviceMessageSend(deviceSn, MsgType.RangeHoodFanControl, msgBody)
        let deviceReplyMsgPredicate = (msg) => {
            // msg 是一个 DeviceMessageReceive 数据类型
            g_utils.writeLog("【油烟机-设置风扇灯光】命令断言函数参数：", msg)
    
            let b = msg.msgBody.result > 0
            if (b) {
                g_utils.writeLog("发送【油烟机-设置风扇灯光】命令成功")
            } else {
                g_utils.writeLog("发送【油烟机-设置风扇灯光】命令失败")
            }
    
            return b
        }
        return this.deviceProtocolStore.sendMessageWithReplyPredicate(deviceMessageSendEntity, deviceReplyMsgPredicate)
    }
    /**
     * 油烟机-关闭油烟机
     * @param {String} deviceSn 设备SN号
     */
    rangeHoodPoweroffControl(deviceSn) {
        g_utils.writeLog("准备发送【油烟机-关闭油烟机】命令给设备：", deviceSn)

        // 封装成发送消息
        let deviceMessageSendEntity = new DeviceMessageSend(deviceSn, MsgType.RangeHoodPowerControl, null)
        let deviceReplyMsgPredicate = (msg) => {
            // msg 是一个 DeviceMessageReceive 数据类型
            g_utils.writeLog("【油烟机-关闭油烟机】命令断言函数参数：", msg)
    
            let b = msg.msgBody.result > 0
            if (b) {
                g_utils.writeLog("发送【油烟机-关闭油烟机】命令成功")
            } else {
                g_utils.writeLog("发送【油烟机-关闭油烟机】命令失败")
            }
    
            return b
        }
        return this.deviceProtocolStore.sendMessageWithReplyPredicate(deviceMessageSendEntity, deviceReplyMsgPredicate)
    }
}

export const deviceProtocolEquipmentService = new DeviceProtocolEquipmentService()